﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data;

namespace DAO
{
    public class ConnectDB
    {
        protected static String _connectionString =
        @"Data Source=.\SQLEXPRESS;AttachDbFilename=\DataBase\QuanLyHocSinh_Data.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
        //@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\HK\CNPM\DA\QLHS\QuanLyHocSinh\DAO\DataBase\QuanLyHocSinh_Data.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
        //Provider = Microsoft.Jet.OleDb.4.0; Data Source=QLNS.mdb
        static OleDbConnection connection = null;
        public static void OpenConnection()
        {
            connection = new OleDbConnection(_connectionString);
            connection.Open();
        }
        public static void CloseConnection()
        {
            if (connection != null)
            {
                connection.Close();
            }
        }
        public static DataTable ExecQuery(string sql)
        {
            OpenConnection();
            DataTable dt = new DataTable();
            OleDbCommand command = connection.CreateCommand();
            command.CommandText = sql;
            OleDbDataAdapter adapter = new OleDbDataAdapter();
            adapter.SelectCommand = command;
            adapter.Fill(dt);
            CloseConnection();
            return dt;
        }
        public static void ExecNonQuery(string sql)
        {
            OpenConnection();
            OleDbCommand command = connection.CreateCommand();
            command.CommandText = sql;
            command.ExecuteNonQuery();
            CloseConnection();
        }
        public static object ExecScalar(string sql)
        {
            OpenConnection();
            OleDbCommand command = connection.CreateCommand();
            command.CommandText = sql;
            object o = command.ExecuteScalar();
            CloseConnection();
            return o;
        }
    }
}
